#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_preopt.info <<'%EOF%'
   geoopt_preopt
   -------------
   Molecule:         H2O
   Wave Function:    SCF / STO-3G, 4-31G and cc-pVDZ
   Test Purpose:     Demonstrate pre-optimization using smaller
                     basis sets. The molecule is optimized using
                     STO-3G, then 6-31G** and finally cc-pVTZ
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_preopt.mol <<'%EOF%'
BASIS
cc-pVTZ
Geometry optimization utilizing pre-optimization with smaller
basis sets
Atomtypes=2
Charge=8.0 Atoms=1
O     0.0 0.3 0.0
Charge=1.0 Atoms=2
H     1.5 -0.9 0.0
H    -1.5 -0.9 0.0
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_preopt.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
*OPTIMIZE
.PREOPT
2
STO-3G
6-31G**
.BAKER
**WAVE FUNCTION
.HF
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_preopt.check
cat >>geoopt_preopt.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

# Optimization setup
CRIT1=`$GREP "Default 1st order method will be used\: * BFGS update" $log | wc -l`
CRIT2=`$GREP "Optimization will be performed in redundant internal coordinates" $log | wc -l`
CRIT3=`$GREP "Model Hessian will be used as initial Hessian" $log | wc -l`
CRIT4=`$GREP "Trust region method will be used to control step \(default\)" $log | wc -l`
CRIT5=`$GREP "Baker.s convergence criteria will be used" $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5`
CTRL[1]=5
ERROR[1]="GEOMETRY OPTIMIZATION NOT SET UP CORRECTLY"

# Preoptimization
CRIT1=`$GREP "Preoptimization will be performed with the basis set\(s\)\:" $log | wc -l`
CRIT2=`$GREP "      STO\-3G" $log | wc -l`
CRIT3=`$GREP "      6\-31G\*\*" $log | wc -l`
TEST[2]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[2]=3
ERROR[2]="PREOPTIMIZATION NOT SET UP CORRECTLY"

# Basis set1
CRIT1=`$GREP "Basis set used is \"STO\-3G\" from the basis set library\." $log | wc -l`
CRIT2=`$GREP "O * 1 * 8\.0000 * 15 * 5 * \[6s3p\|2s1p\]" $log | wc -l`
CRIT3=`$GREP "H * 2 * 1\.0000 * 3 * 1 * \[3s\|1s\]" $log | wc -l`
CRIT4=`$GREP "total\: * 3 * 10\.0000 * 21 * 7" $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[3]=4
ERROR[3]="FIRST BASIS SET NOT READ CORRECTLY"

# Symmetry
CRIT1=`$GREP "Number of orbitals in each symmetry\: * 4 * 1 * 2 * 0" $log | wc -l`
TEST[4]=`expr	$CRIT1`
CTRL[4]=1
ERROR[4]="SYMMETRY NOT CORRECT"

# Initial energy
CRIT1=`$GREP "Final * HF energy\: * \-74\.963868171[89]" $log | wc -l`
TEST[5]=`expr	$CRIT1`
CTRL[5]=1
ERROR[5]="INITIAL ENERGY NOT CORRECT"

# Initial gradient
CRIT1=`$GREP "O * z * \-0*\.02513" $log | wc -l`
CRIT2=`$GREP "H * y * 0*\.05956" $log | wc -l`
CRIT3=`$GREP "H * z * 0*\.02513" $log | wc -l`
TEST[6]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[6]=3
ERROR[6]="INITIAL GRADIENT NOT CORRECT"

# Initial step
CRIT1=`$GREP "O * 0*.0000000000 * 0*\.0000000000 * \-0*\.1444[12]" $log | wc -l`
CRIT2=`$GREP "H * _1 * 0*.0000000000 * 1\.42132.* * 1\.07076" $log | wc -l`
CRIT3=`$GREP "H * _2 * 0*.0000000000 * \-1\.42132.* * 1\.07076" $log | wc -l`
TEST[7]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[7]=3
ERROR[7]="INITIAL STEP NOT CORRECT"

# Second iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-74\.96584[0-9]" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.00582[0-9]" $log | wc -l`
CRIT3=`$GREP "Norm of step * \: * (0| )\.02916[0-9]" $log | wc -l`
TEST[8]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[8]=3
ERROR[8]="SECOND ITERATION NOT CORRECT"

# Third iteration
CRIT1=`$GREP "Norm of gradient * \: * 0*\.00053" $log | wc -l`
CRIT2=`$GREP "Norm of step * \: *     0*\.000(79|80)" $log | wc -l`
TEST[9]=`expr	$CRIT1 \+ $CRIT2`
CTRL[9]=2
ERROR[9]="THIRD ITERATION NOT CORRECT"

# Basis set2
CRIT1=`$GREP "Basis set used is \"6\-31G\*\*\" from the basis set library\." $log | wc -l`
CRIT2=`$GREP "O * 1 * 8\.0000 * 27 * 14 * \[10s4p1d\|3s2p1d\]" $log | wc -l`
CRIT3=`$GREP "H * 2 * 1\.0000 * 7 * 5 * \[4s1p\|2s1p\]" $log | wc -l`
CRIT4=`$GREP "total\: * 3 * 10\.0000 * 41 * 24" $log | wc -l`
TEST[10]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[10]=4
ERROR[10]="SECOND BASIS SET NOT READ CORRECTLY"

# First energy with second set
CRIT1=`$GREP "Final * HF energy\: * \-76\.018261" $log | wc -l`
TEST[11]=`expr	$CRIT1`
CTRL[11]=1
ERROR[11]="FIRST ENERGY WITH SECOND BASIS SET NOT CORRECT"

# Basis set3
CRIT1=`$GREP "Basis set used is \"cc\-pVTZ\" from the basis set library\." $log | wc -l`
CRIT2=`$GREP "O * 1 * 8\.0000 * 42 * 30 * \[10s5p2d1f\|4s3p2d1f\]" $log | wc -l`
CRIT3=`$GREP "H * 2 * 1\.0000 * 16 * 14 * \[5s2p1d\|3s2p1d\]" $log | wc -l`
CRIT4=`$GREP "total\: * 3 * 10\.0000 * 74 * 58" $log | wc -l`
TEST[12]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[12]=4
ERROR[12]="FINAL BASIS SET NOT READ CORRECTLY"

# First energy with third set
CRIT1=`$GREP "Final * HF energy\: * \-76\.057759" $log | wc -l`
TEST[13]=`expr	$CRIT1`
CTRL[13]=1
ERROR[13]="FIRST ENERGY WITH THIRD BASIS SET NOT CORRECT"

# Final geometry
CRIT1=`$GREP -l "O *      (0| )\.0000000000 * (0| )\.0000000000 * \-0*\.0474[56]" $log | wc -l`
CRIT2=`$GREP -l "H * _1 * (0| )\.0000000000 *   1\.4196[45].* 1.02228" $log | wc -l`
CRIT3=`$GREP -l "H * _2 * (0| )\.0000000000 * \-1\.4196[45].* 1.02228" $log | wc -l`
TEST[14]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[14]=3
ERROR[14]="FINAL GEOMETRY NOT CORRECT"

# Convergence
CRIT1=`$GREP "Geometry converged in * 11( |  )iterations\!" $log | wc -l`
CRIT2=`$GREP "Energy at final geometry is * \: * \-76\.05777[0-9] a\.u\." $log | wc -l`
CRIT3=`$GREP "Energy change during optimization \: * \-1\.09390[0-9] a\.u\." $log | wc -l`
TEST[15]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[15]=3
ERROR[15]="GEOMETRY OPTIMIZATION NOT CONVERGED"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} )"
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
